//
//  DeviceRequest.m
//  Cycling
//
//  Created by chijianguo on 2023/4/29.
//

#import "DeviceRequest.h"
#import "BaseBodyModel.h"

@implementation DeviceRequest
// 绑定设备
+ (void)bindingDeviceWithSn:(NSString *)sn name:(NSString *)name model:(NSString *)model result:(void(^)(BOOL ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_BindingDevice];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:sn forKey:@"sn"];
    [parm setObject:name forKey:@"name"];
    [parm setObject:model==nil? @"" : model forKey:@"model"];
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(NO);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                resultBlock(model.code == 1);
            }
        }
    }];
}

// 更新设备数据
+ (void)updateDeviceDataWithSn:(NSString *)sn name:(NSString *)name model:(NSString *)model data:(NSDictionary *)data result:(void(^)(BOOL ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_UpdateDeviceData];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:sn forKey:@"sn"];
    [parm setObject:name forKey:@"name"];
    [parm setObject:model==nil ? @"" : model forKey:@"model"];
    for (int i = 0; i < data.allKeys.count; i++) {
        NSString *key = data.allKeys[i];
        [parm setObject:data.allValues[i] forKey:key];
    }

    NSDictionary *parm1 = @{
                           @"flag":@"更新蓝牙数据接口",
                           @"content":[NSString stringWithFormat:@"接口参数：%@",[self parameterToString:parm]]
                           };
    NSString *url1 = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_Test];
    [BaseRequest baseGET_New:url1 cache:NO autoHideLoading:NO parms:parm1 request:^(id  _Nullable resultObject, NSError * _Nullable error) {
    }];

    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(NO);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                resultBlock(model.code == 1);
            }
        }
    }];
}
// 字典转json字符串
+(NSString *)parameterToString:(NSDictionary *)dic {
    NSString *param = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
    param = [param stringByReplacingOccurrencesOfString:@" " withString:@""];
    param = [param stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    param = [param stringByReplacingOccurrencesOfString:@"\n" withString:@""];

    return param;
}

// 解绑设备
+ (void)bindingDeviceWithId:(NSString *)eid result:(void(^)(BOOL ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_UnBindingDevice];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:eid forKey:@"id"];
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(NO);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                resultBlock(model.code == 1);
            }
        }
    }];
}

@end
